<!--
 * @Author: hucc
 * @Date: 2021-09-16 17:09:01
 * @LastEditors: hucc
 * @LastEditTime: 2021-09-16 17:20:26
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
    window.onload = function() {
        //获取发送按钮
        var sendBtn = document.getElementById("send");
        //给发送数据按钮绑定事件
        sendBtn.onclick = function() {
                var obj = {
                        name: "zs",
                        age: 12
                    }
                    //发送的消息 目标源:可写可不写，会默认显示
                win.postMessage(obj);
            }
            //给内联框架B1发送消息
        var foo = document.getElementById("btn");
        foo.onclick = function() {
            //获取B1内联窗口 给B1发送数据
            win = document.querySelector("iframe").contentWindow;
        }
    }
</script>

<body>

    <button id="send">发送数据</button>
    <button id="btn">获取内联框架B1，给B1发送消息</button>
    <iframe src="./11-B1.html" frameborder="0"></iframe>
</body>

</html>